//
//  HDRootNewsVC.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/4/10.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

@interface HDNewsDetail : NSObject
@property (nonatomic, strong) NSString *date;
@property (nonatomic, strong) NSString *list_img;
@property (nonatomic, strong) NSString *news_id;
@property (nonatomic, strong) NSString *title;

+ (instancetype)newsDetailWithDict:(NSDictionary *)dict;

@end
@implementation HDNewsDetail

+ (instancetype)newsDetailWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {

        [self setValuesForKeysWithDictionary:dict]; // 字典的快速赋值，需要特殊处理一下保留字段和未声明但是字典中有的字段
        if(![_date isKindOfClass:[NSString class]])
            self.date = @"";

    }
    return self;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if([key isEqualToString:@"id"]) {
    }
}

@end


@interface HDNewsObj : NSObject
@property (nonatomic, strong) NSMutableArray *img_listArray;
@property (nonatomic, strong) NSMutableArray *news_listArray;
+ (instancetype)newsObjWithDict:(NSDictionary *)dict;

@end

@implementation HDNewsObj

+ (instancetype)newsObjWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict]; // 字典的快速赋值，需要特殊处理一下保留字段和未声明但是字典中有的字段
        [self arrayClean];
        _img_listArray  = [NSMutableArray new];
        _news_listArray = [NSMutableArray new];
        
        NSArray *imgArray = dict[@"img_list"];
        for (NSInteger i=0;i<imgArray.count;i++) {
            NSString *path = imgArray[i];
            [_img_listArray addObject:path];
        }
        
        NSArray *newsArray = dict[@"news_list"];
        for (NSInteger i=0;i<newsArray.count;i++) {
            NSDictionary *objDic = newsArray[i];
            HDNewsDetail *objDetail = [HDNewsDetail newsDetailWithDict:objDic];
            [_news_listArray addObject:objDetail];
        }
    }
    return self;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if([key isEqualToString:@"id"]) {
    }
}

- (void)arrayClean
{
    [_img_listArray removeAllObjects];
    _img_listArray = nil;
    [_news_listArray removeAllObjects];
    _news_listArray = nil;
}

- (void)dealloc
{
    [self arrayClean];
}

@end
#import "HDRootNewsVC.h"
#import "HDBottomBarView.h"
#import "HDRootNewsTopView.h"
#import "HDWebVC.h"

@interface HDRootNewsVC ()<HDBottomBarViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) HDNewsObj *myNewsObj;

@end

@implementation HDRootNewsVC

#pragma mark
#pragma mark -------- Init Methods --------

- (void)viewDidLoad {
    [super viewDidLoad];
    [self barInit];
    [self tableviewInit];
    
    [self newsRequestAction];
}

- (void)barInit
{
    HDBottomBarView *bar = [[[NSBundle mainBundle] loadNibNamed:@"HDBottomBarView" owner:self options:nil] lastObject];
    [_bottomView addSubview:bar];
    [bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bottomView).with.offset(0);
        make.right.equalTo(_bottomView).with.offset(0);
        make.left.equalTo(_bottomView).with.offset(0);
        make.bottom.equalTo(_bottomView).with.offset(0);
    }];
    [bar hightedBtn:1];
    bar.delegate = self;
}

- (void)barSelectAction:(NSString *)VCStr
{
    NSArray *vcArray = self.navigationController.viewControllers;
    BOOL isVCExit = NO;
    for (UIViewController *vc in vcArray) {
        if ([vc isKindOfClass:NSClassFromString(VCStr)]) {
            isVCExit = YES;
            [self.navigationController popToViewController:vc animated:NO];
            break;
        }
    }
    
    if (isVCExit == NO) {
        UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *pushVC = [board instantiateViewControllerWithIdentifier:VCStr];
        [self.navigationController pushViewController:pushVC animated:NO];
    }
}

- (void)newsRequestAction {
    
    HDRequestObj *loginObj = [[HDRequestObj alloc] init];
    NSString *urlPath = [NSString stringWithFormat:@"%@g=mapi&m=Contentinfo&a=news_info", HTTP_Request_URL_Prefix];
    NSDictionary *parameters = @{@"page_id": @"CHINESE",
                                 @"page_size": @""};
    
    [loginObj requestGetPath:urlPath Parameter:parameters withVC:self timeout:5];
    loginObj.myDataBlock = ^(NSDictionary *dataDic){
        
        NSString *loginCode = dataDic[@"status"];
        if (loginCode.integerValue == 1) {
            NSDictionary *newsDic = dataDic[@"data"];
            _myNewsObj = [HDNewsObj newsObjWithDict:newsDic];
            [_tableView reloadData];
        }
        else {
        }
    };
}

#pragma mark
#pragma mark -------- Tableview Methods --------

#define News_Cell_Height 60
#define Scroll_Img_Tag_Pre 1100
#define Header_Height (200*ScreenWidth/320)
- (void)tableviewInit {
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = News_Cell_Height;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return Header_Height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HDRootNewsTopView *topView = [[[NSBundle mainBundle] loadNibNamed:@"HDRootNewsTopView" owner:self options:nil] lastObject];
    
    NSInteger imgCnt = _myNewsObj.img_listArray.count;
    topView.scrollView.contentSize = CGSizeMake(imgCnt*ScreenWidth, Header_Height);
    [topView resetPageCnt:imgCnt];
    
    for (NSInteger i=0;i<imgCnt;i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth*i, 0, ScreenWidth, Header_Height)];
        imgView.tag = Scroll_Img_Tag_Pre + i;
        [imgView sd_setImageWithURL:[NSURL URLWithString:_myNewsObj.img_listArray[i]] placeholderImage:[UIImage imageNamed:@"rootvc_bg"]];
        
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgTap:)];
        [imgView addGestureRecognizer:myTap];
        imgView.userInteractionEnabled = YES;
        [topView.scrollView addSubview:imgView];
    }
    
    return topView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ return  1; }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _myNewsObj.news_listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"rootNewsListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    HDNewsDetail *obj = _myNewsObj.news_listArray[indexPath.row];
    cell.textLabel.text = obj.title;
    cell.textLabel.font = [UIFont fontWithName:@"FangSong" size:15.0f];
    cell.detailTextLabel.text = obj.date;
    UIImageView *rightImgView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 10, 5, (News_Cell_Height-10)/3*4, News_Cell_Height-10)];
    [rightImgView sd_setImageWithURL:[NSURL URLWithString:obj.list_img] placeholderImage:[UIImage imageNamed:@"player_default"]];
    rightImgView.contentMode = UIViewContentModeScaleAspectFit;
    cell.accessoryView = rightImgView;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HDNewsDetail *obj = _myNewsObj.news_listArray[indexPath.row];

    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    HDWebVC *webVC = [board instantiateViewControllerWithIdentifier:@"HDWebVC"];
    webVC.newsID = obj.news_id;
    [self.navigationController pushViewController:webVC animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark
#pragma mark -------- Dealloc Methods --------

- (void)imgTap:(UITapGestureRecognizer *)myTap
{
    UIView *tempView = myTap.view;
    NSInteger viewIndex = tempView.tag - Scroll_Img_Tag_Pre;
    if (viewIndex >= 0 && viewIndex < _myNewsObj.news_listArray.count) {
        HDNewsDetail *obj = _myNewsObj.news_listArray[viewIndex];
        
        UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        HDWebVC *webVC = [board instantiateViewControllerWithIdentifier:@"HDWebVC"];
        webVC.newsID = obj.news_id;
        [self.navigationController pushViewController:webVC animated:YES];
    }
}

- (void)dealloc
{
    _myNewsObj = nil;
}



@end
